package com.web.controller;
import com.web.common.Result;
import com.web.entity.RecognitionHistory;
import com.web.service.RecognitionHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
@RestController
@RequestMapping("/history")  // 🔧 确保路径正确
@CrossOrigin
public class HistoryController {

    @Autowired
    private RecognitionHistoryService historyService;

    // 🔥 获取历史记录列表
    @GetMapping("/list")
    public Result<IPage<RecognitionHistory>> getHistoryList(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String imageUrl,
            @RequestParam(required = false) String keyword) {

        System.out.println("=== 获取历史记录列表 ===");
        System.out.println("用户ID: " + userId);
        System.out.println("页码: " + page);
        System.out.println("页大小: " + size);
        System.out.println("类型筛选: " + type);
        System.out.println("关键词: " + keyword);
        System.out.println("图片URL筛选: " + imageUrl);

//        try {
//            IPage<RecognitionHistory> result = historyService.getHistoryList(userId, page, size, type, keyword);
//
//            System.out.println("查询结果总数: " + result.getTotal());
//            System.out.println("当前页记录数: " + result.getRecords().size());
//
//            return Result.success("获取历史记录成功", result);
//        } catch (Exception e) {
//            System.err.println("获取历史记录失败: " + e.getMessage());
//            e.printStackTrace();
//            return Result.fail("获取历史记录失败: " + e.getMessage());
//        }
//    }
        try {
            IPage<RecognitionHistory> result = historyService.getHistoryList(userId, page, size, type, keyword, imageUrl);
            System.out.println("查询结果总数: " + result.getTotal());
            System.out.println("当前页记录数: " + result.getRecords().size());

            return Result.success("获取历史记录成功", result);
        } catch (Exception e) {
            System.err.println("获取历史记录失败: " + e.getMessage());
            e.printStackTrace();
            return Result.fail("获取历史记录失败: " + e.getMessage());
        }
    }

    // 🔥 获取历史详情
    @GetMapping("/detail/{id}")
    public Result<RecognitionHistory> getHistoryDetail(@PathVariable Long id) {
        System.out.println("=== 获取历史详情 ===");
        System.out.println("记录ID: " + id);

        try {
            RecognitionHistory history = historyService.getHistoryDetail(id);
            if (history != null) {
                System.out.println("找到历史记录: " + history.getResult());
                return Result.success("获取历史详情成功", history);
            } else {
                System.out.println("历史记录不存在");
                return Result.fail("记录不存在");
            }
        } catch (Exception e) {
            System.err.println("获取历史详情失败: " + e.getMessage());
            e.printStackTrace();
            return Result.fail("获取历史详情失败: " + e.getMessage());
        }
    }

    // 🔥 删除历史记录
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteHistory(@PathVariable Long id, @RequestParam Long userId) {
        System.out.println("=== 删除历史记录 ===");
        System.out.println("记录ID: " + id);
        System.out.println("用户ID: " + userId);

        try {
            boolean success = historyService.deleteHistory(id, userId);
            if (success) {
                System.out.println("删除成功");
                return Result.success("删除成功");
            } else {
                System.out.println("删除失败");
                return Result.fail("删除失败");
            }
        } catch (Exception e) {
            System.err.println("删除历史记录失败: " + e.getMessage());
            e.printStackTrace();
            return Result.fail("删除失败: " + e.getMessage());
        }
    }

    // 🔥 清空所有历史
    @DeleteMapping("/clear")
    public Result<String> clearAllHistory(@RequestParam Long userId) {
        System.out.println("=== 清空所有历史记录 ===");
        System.out.println("用户ID: " + userId);

        try {
            boolean success = historyService.clearAllHistory(userId);
            if (success) {
                System.out.println("清空成功");
                return Result.success("清空成功");
            } else {
                System.out.println("清空失败");
                return Result.fail("清空失败");
            }
        } catch (Exception e) {
            System.err.println("清空历史记录失败: " + e.getMessage());
            e.printStackTrace();
            return Result.fail("清空失败: " + e.getMessage());
        }
    }
}
